package com.sen.organization.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.response.ResultMsgEnum;
import com.sen.common.common.util.CheckUtils;
import com.sen.common.user.config.UserContextHolder;
import com.sen.organization.dto.MemberDepartmentDTO;
import com.sen.organization.service.DepartmentService;
import com.sen.organization.vo.MemberDepartmentVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

import static com.sen.organization.enums.MemberEnum.add_department_failure;

@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {
    /**
     * 查询列表
     * @param searchDTO
     * @return
     */
    @Resource
    DepartmentService departmentService;

    @GetMapping("/listDepartTree")
    public MessageResult listDepartTree(){
        return exec("查询部门树",null,()->{
            MessageResult messageResult=new MessageResult();
            String entityCode= UserContextHolder.getPeople().getEntityCode();
            Set<MemberDepartmentVo> list = departmentService.listDepartTree(entityCode);
            if (CollectionUtil.isNotEmpty(list)){
                messageResult = MessageResult.success(list);
            }else {
                messageResult = MessageResult.success(null);
            }
            return messageResult;
        }) ;
    }

    @PostMapping("/add")
    public MessageResult add(@RequestBody MemberDepartmentDTO departmentDto){
        return exec("添加部门",null,()->{
            CheckUtils.checkNotNull(departmentDto);
            String entityCode=UserContextHolder.getPeople().getEntityCode();
            departmentDto.setEntityCode(entityCode);
            if (departmentService.addDepartment(departmentDto)){
                return MessageResult.success(null) ;
            }else {
                return MessageResult.messageResult(null, ResultMsgEnum.SUCCESS, add_department_failure) ;
            }
        }) ;
    }

    @PostMapping("/update")
    public MessageResult update(@RequestBody MemberDepartmentDTO departmentDto){
        return exec("更新部门",null,()->{
            CheckUtils.checkNotNull(departmentDto);
            departmentDto.setEntityCode(UserContextHolder.getPeople().getEntityCode());
            if (departmentService.addDepartment(departmentDto)){
                return MessageResult.success(null) ;
            }
            return MessageResult.bizFailed(null) ;
        }) ;
    }

    /**
     * 查询列表
     * @param searchDTO
     * @return
     */
    @PostMapping("/list")
    public MessageResult list(@RequestBody MemberDepartmentDTO searchDTO){
        return exec("查询部门列表",null,()->{
            String entityCode=UserContextHolder.getPeople().getEntityCode();
            searchDTO.setEntityCode(entityCode);
            return MessageResult.success(departmentService.list(searchDTO));
        }) ;
    }

    /**
     * 删除部门
     * @param code
     * @return
     */
    @GetMapping("/del")
    public MessageResult del(@RequestParam("code") String code){
        return exec("删除部门",code,()->{
            departmentService.del(code);
            return MessageResult.success() ;
        }) ;
    }

    /**
     * 查询部门详细信息
     * @param code
     * @return
     */
    @GetMapping("/get")
    public MessageResult get(@RequestParam("code") String code){
        return exec("查询部门详细信息",code,()->
            MessageResult.success(departmentService.getMemberDepartment(code))
        ) ;
    }

}
